package com.trkj.service.cf;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.trkj.dao.cf.CertificateTypeDAO;
import com.trkj.dao.cf.ZszlTypeDAO;
import com.trkj.pojo.CertificateType;
import com.trkj.pojo.ZszlType;
import com.trkj.service.ServiceException;
import com.trkj.vo.cf.CertificateTypeVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class CertificateTypeService {
	@Autowired
	private CertificateTypeDAO certificateTypeDAO;
	@Autowired
	private ZszlTypeDAO zszlTypeDAO;
	public long pageCount() {
				return zszlTypeDAO.count();
	}
	public long pageCount1() {
		return certificateTypeDAO.count();
	}
	//证书查询
	public List<CertificateTypeVo> certificateTypePage(String certypeName, int page, int rows) {
		List<Object[]> map = certificateTypeDAO.queryBycertypeName("%"+certypeName+"%", new PageRequest(page-1, rows));
		List<CertificateTypeVo> list = new ArrayList<>();
		if(map != null){
			for(Object[] objs: map){
				ZszlType z=(ZszlType) objs[0];
				CertificateType ct = (CertificateType) objs[1];
				CertificateTypeVo vo = new CertificateTypeVo();
				vo.setCertypeId(ct.getCertypeId());
				vo.setCertypeName(ct.getCertypeName());
				vo.setCertypeRemark(ct.getCertypeRemark());
				vo.setHasSeveral(ct.getHasSeveral());
				vo.setMinDemand(ct.getMinDemand());
				vo.setZszlId(z.getZszlId());
				vo.setZszlName(z.getZszlName());
				list.add(vo);
			}
		}
		return list;
	}
	 
	 
	//添加
	public void certificateTypeadd(CertificateTypeVo vo) throws ServiceException{
			try {
				CertificateType c= certificateTypeDAO.findOne(vo.getZszlId());
					ZszlType z=new ZszlType();
					System.out.println(vo.getZszlName()+"种类");
					z.setZszlName(vo.getZszlName());
					z.setCertificateType(c);
					zszlTypeDAO.save(z);
			} catch (Exception e) {
				throw new ServiceException();
			}
	}
	//编辑
	public void certificateTypeUpdate(CertificateTypeVo vo) throws ServiceException {
		try {
			CertificateType c= certificateTypeDAO.findOne(vo.getCertypeId());
				ZszlType z= zszlTypeDAO.findOne(vo.getZszlId());
				z.setZszlName(vo.getZszlName());
				z.setCertificateType(c);
				zszlTypeDAO.save(z);
		} catch (Exception e) {
			throw new ServiceException();
		}
	}
	//删除
	public void certificateclassifyremove(Long certypeId) {
		zszlTypeDAO.delete(certypeId);
	}
		
}
